package com.shanpin.shebao.admin.module.city;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.shanpin.core.constant.KeyConst;
import com.shanpin.shebao.admin.module.keyvalue.KeyValueService;
import com.shanpin.shebao.common.URI;
import com.shanpin.shebao.model.CitySite;

/**
 * 城市公积金、社保查询网站信息配置
 * @author 品讯科技
 *
 */
@Controller
public class SiteController {

	@Autowired
	private SiteService siteService;
	@Autowired
	private KeyValueService keyValueService;
	
	@RequestMapping(URI.SITE_CFG_LIST)
	public ModelAndView list(String pcity_code) {
		ModelAndView mav = new ModelAndView("setting/sites");
		mav.addObject("datas", siteService.list());
		mav.addObject("version_site_value", keyValueService.get(KeyConst.VERSION_QUERY.getCode()));

		mav.addObject("list_uri", URI.SBA + URI.SITE_CFG_LIST);
		mav.addObject("view_uri", URI.SBA + URI.SITE_CFG_VIEW);
		mav.addObject("key_value_setting", URI.SBA+URI.KEY_VALUE_SETTING);
		return mav;
	}
	
	
	@RequestMapping(URI.SITE_CFG_VIEW)
	public ModelAndView toModify(Integer cid) {
		if (null == cid) {
			cid = 0;
		}
		CitySite data = siteService.get(cid);
		if (null == data) {
			data = new CitySite();
		}
		ModelAndView mav = new ModelAndView("setting/site_modify");
		mav.addObject("data", data);
		
		mav.addObject("list_uri", URI.SBA + URI.SITE_CFG_LIST);
		mav.addObject("view_uri", URI.SBA + URI.SITE_CFG_VIEW);
		mav.addObject("modify_uri", URI.SBA + URI.SITE_CFG_MODIFY);
		mav.addObject("del_uri", URI.SBA + URI.SITE_CFG_DELETE);
		return mav;
	}
	

	@RequestMapping(URI.SITE_CFG_MODIFY)
	public Object modify(CitySite cs) {
		boolean r = siteService.update(cs);
		if (!r) {
			ModelAndView mav = new ModelAndView("error");
			mav.addObject("msg", "配置出错");
			return mav;
		}
		return "redirect:"+URI.SBA + URI.SITE_CFG_VIEW+"?cid="+cs.getCid();
	}
	
	@RequestMapping(URI.SITE_CFG_DELETE)
	public Object del(Integer cid) {
		if (null == cid || cid.intValue() == 0) {
			ModelAndView mav = new ModelAndView("error");
			mav.addObject("msg", "标识无效");
			return mav;
		}
		boolean r = siteService.delete(cid);
		if (!r) {
			ModelAndView mav = new ModelAndView("error");
			mav.addObject("msg", "删除出错了。");
			return mav;
		}
		return "redirect:"+ URI.SBA+URI.SITE_CFG_LIST;
	}
}
